package com.edushare.common.util;

public class SettingsStringHelper {
	StringBuffer settingsString=null;
	
	public SettingsStringHelper(String settingsString){
		this.settingsString = new StringBuffer(settingsString);
	}
	
	public String getSettingsString(){
		return settingsString.toString();
	}
	
	public void setParam(String param, int value){
		if(getParam(param)==-1)
			settingsString.append(param).append(":").append(value).append(";");
		else{
			//Break buffer into three parts. Change the second buffer. cat them.
			int startIndex = settingsString.indexOf(param);
			int endIndex = settingsString.indexOf(";", startIndex);
			String str1 = "";
			String str2 = param +":"+value+";";
			String str3 = settingsString.substring(endIndex, settingsString.length());
			if(startIndex != 0){
				 str1 = settingsString.substring(0, startIndex-1);
			}
			settingsString.setLength(0);
			settingsString.append(str1).append(str2).append(str3);
		}
	}
	
	public int getParam(String param){
		int startIndex = settingsString.indexOf(param);
		if(startIndex == -1)
			return -1;
		int middleIndex = settingsString.indexOf(":", startIndex);
		int endIndex = settingsString.indexOf(";", startIndex);	
		return Integer.parseInt(settingsString.substring(middleIndex+1, endIndex-1));
	}	
}
